package com.example.lt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.lt.constant.MessageConstant;
import com.example.lt.constant.MinioPicPathConstant;
import com.example.lt.constant.PicPathConstant;
import com.example.lt.exception.AdminOperateException;
import com.example.lt.exception.MinioOperateException;
import com.example.lt.mapper.CollectMapper;
import com.example.lt.mapper.ListSongMapper;
import com.example.lt.mapper.RankListMapper;
import com.example.lt.mapper.SongListMapper;
import com.example.lt.pojo.entity.Collect;
import com.example.lt.pojo.entity.ListSong;
import com.example.lt.pojo.entity.RankList;
import com.example.lt.pojo.entity.SongList;
import com.example.lt.pojo.vo.SongListVO;
import com.example.lt.result.Result;
import com.example.lt.service.ListSongService;
import com.example.lt.service.MinioService;
import com.example.lt.service.SongListService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 小涛Tao
 * @Date: 2024/05/31/13:41
 * version 1.0 注释：
 **/
@Service
@RequiredArgsConstructor
public class SongListServiceImpl extends ServiceImpl<SongListMapper, SongList> implements SongListService {

    private final SongListMapper songListMapper;

    private final ListSongMapper listSongMapper;

    private final CollectMapper collectMapper;

    private final RankListMapper rankListMapper;

    private final MinioService minioService;

    /**
     * 管理端更新歌单的图片
     * @param imgFile
     * @param id
     * @return
     */
    public Result updateSongListImg(MultipartFile imgFile, Integer id) {
        String originalFilename = imgFile.getOriginalFilename();
        String newImgPath = PicPathConstant.SONG_LIST_PIC_PATH + originalFilename;

        boolean flag = lambdaUpdate().eq(SongList::getId, id).set(SongList::getPic, newImgPath).update();

        String s = minioService.uploadFile(MinioPicPathConstant.SONG_LIST_IMAGES_PATH, originalFilename, imgFile);

        if (s.equals("File uploaded successfully.") && flag) {
            return Result.success(MessageConstant.ALTER_SUCCESS, newImgPath);
        } else {
            throw new MinioOperateException(MessageConstant.ALTER_FAIL);
        }
    }

    /**
     * 删除歌单，同时删除歌单与歌曲的关联记录、还有对歌单的 评论 和 评分
     * @param id
     * @return
     */
    public Result deleteSongList(Integer id) {
        int flag = songListMapper.deleteById(id);
        if (flag == 0) {
            throw new AdminOperateException(MessageConstant.DELETE_FAIL);
        }

        listSongMapper.delete(new QueryWrapper<ListSong>().eq("song_list_id", id)); // 删除歌单与歌曲的关联记录

        collectMapper.delete(new QueryWrapper<Collect>().eq("song_list_id", id)); // 删除歌单的评论

        rankListMapper.delete(new QueryWrapper<RankList>().eq("song_list_id", id)); // 删除歌单的评分

        return Result.success(MessageConstant.DELETE_SUCCESS);
    }

    /**
     * 查询所有歌单
     * @return
     */
    public List<SongListVO> getSongListPicPath() {
        List<SongList> songLists = songListMapper.selectList(null);

        List<SongListVO> listVOS = new ArrayList<>();
        for (SongList songList : songLists) {
            SongListVO songListVO = new SongListVO();
            BeanUtils.copyProperties(songList, songListVO); // 把SongList中的属性复制到SongListVO中
            listVOS.add(songListVO);
        }

        return listVOS;
    }

    /**
     * 根据歌单风格获取歌单
     * @param style
     * @return
     */
    public Result<List<SongList>> getListByStyle(String style) {
        List<SongList> songLists = lambdaQuery()
                .like(style != null, SongList::getStyle, style)
                .list();
        return Result.success(songLists);
    }

    /**
     * 根据歌单标题模糊查询歌单信息
     * @param title
     * @return
     */
    public Result<List<SongList>> getListLikeTitle(String title) {
        List<SongList> songLists = lambdaQuery()
                .like(title != null, SongList::getTitle, title)
                .list();
        return Result.success(songLists);
    }
}
